package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/18 9:19
 */
public class Flink03_State_Keyed_Value {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env.enableCheckpointing(1000);// checkpoint周期
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String line) throws Exception {
                    String[] data = line.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            .flatMap(new RichFlatMapFunction<WaterSensor, String>() {
                private ValueState<Integer> lastVcState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    lastVcState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("lastVcState", Integer.class));
                    
                }
    
    
                @Override
                public void flatMap(WaterSensor value, Collector<String> out) throws Exception {
                    // 如果状态中有值, 表示这次不是第一次, 判断上次和这次是否都大于10
                    Integer lastVc = lastVcState.value();
                    if (lastVc != null) {
                        if (lastVc > 10 && value.getVc() > 10) {
                            out.collect(value.getId() + " 连续两次水位超过10, 发出红色预警");
                        }
                    }
                    
                    lastVcState.update(value.getVc());
                }
            })
            .print();
            
            
           /* .process(new KeyedProcessFunction<String, WaterSensor, String>() {
    
                private ValueState<Integer> lastVcState;
    
                @Override
                public void open(Configuration parameters) throws Exception {
                    lastVcState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("lastVcState", Integer.class));
                }
    
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    // 如果状态中有值, 表示这次不是第一次, 判断上次和这次是否都大于10
                    Integer lastVc = lastVcState.value();
                    if (lastVc != null) {
                        if (lastVc > 10 && value.getVc() > 10) {
                            out.collect(ctx.getCurrentKey() + " 连续两次水位超过10, 发出红色预警");
                        }
                    }
    
                    lastVcState.update(value.getVc());
                    
                }
            })
            .print();*/
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

